package com.example.myapplication.arouter

import android.content.Context
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Interceptor
import com.alibaba.android.arouter.facade.callback.InterceptorCallback
import com.alibaba.android.arouter.facade.template.IInterceptor
import com.alibaba.android.arouter.launcher.ARouter
import com.alibaba.android.arouter.utils.TextUtils
import com.kun.base.env.Constants
import com.kun.base.util.MMKVUtils

/**
 *登录拦截器
 *
 * @author : Kun
 * @date : 2021/6/7
 * @time : 16:44
 */
@Interceptor(priority = 1)
class LoginInterceptor : IInterceptor {
    override fun init(context: Context?) {
    }

    override fun process(postcard: Postcard, callback: InterceptorCallback) {
        val path = postcard.path
        if (path.contains("/needLogin/") && TextUtils.isEmpty(MMKVUtils.getString(Constants.USERNAME_CACHE_KEY))) {
            //需要登录切未登录
            callback.onInterrupt(null)
            ARouter.getInstance().build(ARouterPath.LOGIN).navigation()
        }
        callback.onContinue(postcard)
    }
}